<template>
  <el-dialog
    :before-close="handleClose"
    :visible.sync="dialogVisible"
    title="提示"
    width="30%"
  >
    <p class="text-danger">
      {{ tips }}
    </p>
    <p style="text-align: center; color: #a1a1a1;">
      {{ percentage >= 100 ? completeMessage : message }}
    </p>
    <el-progress :percentage="percentage" />
    <el-link v-if="downloadUrl" type="primary" class="download" @click="downloadExport(downloadUrl)">
      下载地址
    </el-link>
    <!-- <el-link v-if="downloadUrl" :href="downloadUrl" type="primary" class="download">
      下载地址
    </el-link> -->
  </el-dialog>
</template>

<script>
import {getProjectRequest} from "../../api/project/form"
export default {
    name: 'HandleProcess',
    props: {},
    data() {
        return {
            tips:'',
            completeMessage: '已完成',
            message: '处理中',
            downloadUrl: '',
            key: '',
            timer: null,
            percentage: 0,
            dialogVisible: true,
            formKey:'',
            formName:''
        }
    },
    created() {
        this.getHandleProcess()
        getProjectRequest(this.formKey).then((res) => {
          this.formName = res.data.name
        })
        this.timer = setInterval(() => {
            this.getHandleProcess()
        }, 5000)
    },
    destroyed() {
        clearInterval(this.timer)
    },
    methods: {
        /**
         * 获取处理进去
         */
        getHandleProcess() {
            this.$api.get(`/common/process?key=${this.key}`).then((res) => {
                let {rate, url} = res.data
                if (rate) {
                    this.percentage = rate
                }
                if (url) {
                    this.downloadUrl = url
                    clearInterval(this.timer)
                }
            })
        },
        handleClose() {
            clearInterval(this.timer)
            this.$destroy(true)
            this.$el.parentNode.removeChild(this.$el)
        },
        downloadExport(url){
          console.log(url,'url')
          const elink = document.createElement('a')
          elink.style.display = 'none'
          elink.href = url
          var name = this.formName.replace(/<[^>]+>/g, '')
          console.log(name,'eeeeeeeee')
          elink.download=name+'_'+this.formatDate(new Date())+'.zip'
          console.log(elink.download,'elink.download')
          document.body.appendChild(elink)
          elink.click()
          document.body.removeChild(elink)
        },
        formatDate (date) {
          let year = date.getFullYear()
          let month = date.getMonth() + 1
          let day = date.getDate()
          return `${year}-${month}-${day}`
        }
          
    }

}
</script>
<style lang="scss" scoped>
.download{
  width: 70px;
  margin: 10px auto;
  display: block;
}
</style>

